<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2023-02-08 15:16:22
 * @Last Modified time: 2023-02-08 15:45:17
 */

namespace app\sys\controller\store;

use app\sys\controller\Base;
use think\facade\{
    App
};
use app\common\services\store\ShopService;
/**
 * 门店
 * Class Shop 
 * @package app\sys\controller\store
 */
class Shop extends Base
{
    /**
     * Shop constructor.
     * @param App $app
     */
    public function __construct(App $app,ShopService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 门店列表
     * @method(GET)
     */
    public function list()
    {
        $where = $this->request->getMore([
        ]);
        $this->success($this->service->getShopList($where));
    }

    /**
     * 门店详细 
     * @method(GET)
     */
    public function detail(){
        $this->success($this->service->getShopDetail($this->request->get('id')));
    }

    /**
     * 添加门店
     * @method(POST)
     */
    public function create()
    {
        $data = $this->request->postMore([
            'shop_name',
            ['phone', ''],
            ['shop_logo', ''],
            ['shop_image', ''],
            ['start_time', ''],
            ['end_time', ''],
            ['is_check', '1'],
            ['location', ''],
            ['address', ''],
            ['region', ''],
        ]);
        if (!$data['shop_name']) $this->error('请输入门店名称！');
        $data = $this->service->getAddressRegionData($data);
        if($this->service->save( $data)){
            $this->success('添加成功!');
        }
        $this->error('添加失败!');
    }

    /**
     * 编辑门店
     * @method(PUT)
     */
    public function update()
    {
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        $data = $this->request->postMore([
            'shop_name',
            ['phone', ''],
            ['shop_logo', ''],
            ['shop_image', ''],
            ['start_time', ''],
            ['end_time', ''],
            ['is_check', '1'],
            ['location', ''],
            ['address', ''],
            ['region', ''],
        ]);
        if (!$data['shop_name']) $this->error('请输入门店名称！');
        $data = $this->service->getAddressRegionData($data);
        if($this->service->update($id, $data)){
            $this->success('修改成功!');
        }
        $this->error('修改失败!');
    }

    /**
     * 删除门店
     * @method(DELETE)
     */
    public function delete()
    {
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }

        if ($this->service->delete((int)$id)) {
            $this->success('删除成功!');
        } 
        $this->error('删除失败!');
    }

    /**
     * 修改门店状态
     * @method(PUT)
     */
    public function status()
    {
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        if ($this->service->update($id, ['status' => $this->request->param('status')])) {
            $this->success('修改成功');
        }
        $this->error('修改失败');
    }
}


